package com.chexiehui.jc.utils;

import android.content.Context;
import android.content.Intent;

import com.blankj.utilcode.util.ToastUtils;

/**
 * Author: SongJun.
 * CreateDate: 2018/10/25 0025.
 * Description:
 */

public class GPSUtil {

    private static final double x_pi = 3.14159265358979324;

    /**
     * 坐标转换，腾讯地图转换成百度地图坐标
     *
     * @param lat 腾讯纬度
     * @param lon 腾讯经度
     * @return 返回结果：经度,纬度
     */
    public static double[] map_tx2bd(double lat, double lon) {
        double bd_lat;
        double bd_lon;
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        bd_lon = z * Math.cos(theta) + 0.0065;
        bd_lat = z * Math.sin(theta) + 0.006;

        System.out.println("bd_lat:" + bd_lat);
        System.out.println("bd_lon:" + bd_lon);
        return new double[]{bd_lat, bd_lon};
    }


    /**
     * 坐标转换，百度地图坐标转换成腾讯地图坐标
     *
     * @param lat 百度坐标纬度
     * @param lon 百度坐标经度
     * @return 返回结果：纬度,经度
     */
    public static String map_bd2tx(double lat, double lon) {
        double tx_lat;
        double tx_lon;
        double x_pi = 3.14159265358979324;
        double x = lon - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        tx_lon = z * Math.cos(theta);
        tx_lat = z * Math.sin(theta);
        return tx_lat + "," + tx_lon;
    }

    public static void gotoMap(Context context, int type, String address, double lat, double lon) {
        switch (type) {
            case 0:
                GPSUtil.gotoTengxun(context, address, lat, lon);
                break;
            case 1:
                GPSUtil.goToNaviActivity(context, address, lat, lon);
                break;
            case 2:
                GPSUtil.goToBaiduActivity(context, address, lat, lon);
                break;
        }
    }


    /**
     * 启动高德App进行导航
     * <p>
     * 必填 第三方调用应用名称。如 amap
     *
     * @param dname 非必填 目的地名称
     * @param dlat  必填 终点纬度
     * @param dlon  必填 终点经度
     *              必填 是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
     *              必填 预设的导航方式 t = 0（驾车）= 1（公交）= 2（步行）= 3（骑行）= 4（火车）= 5（长途客车）
     */
    public static void goToNaviActivity(Context context, String dname, double dlat, double dlon) {
        try {
            //启动路径规划页面
            String uri = "amapuri://route/plan/?dlat=" + dlat + "&dlon=" + dlon + "&dname=" + dname + "&dev=1&t=0";
            Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(uri));
            intent.setPackage("com.autonavi.minimap");
            context.startActivity(intent);
        } catch (Exception e) {
            ToastUtils.showShort("请先安装高德地图客户端");
        }
    }

    /**
     * 启动腾讯地图App进行导航
     *
     * @param address 目的地
     * @param lat     必填 纬度
     * @param lon     必填 经度
     */
    public static void gotoTengxun(Context context, String address, double lat, double lon) {
        try {
            // 启动路径规划页面
            Intent naviIntent = new Intent("android.intent.action.VIEW",
                    android.net.Uri.parse("qqmap://map/routeplan?type=drive&from=&fromcoord=&to=" + address +
                            "&tocoord=" + lat + "," + lon + "&policy=0&referer=appName"));
            context.startActivity(naviIntent);
        } catch (Exception e) {
            ToastUtils.showShort("请先安装腾讯地图客户端");
        }
    }

    /**
     * 启动百度App进行导航
     *
     * @param address 目的地
     * @param lat     必填 纬度
     * @param lon     必填 经度
     */
    public static void goToBaiduActivity(Context context, String address, double lat, double lon) {
        try {
            double[] doubles = gcj02_To_Bd09(lat, lon);
            //启动路径规划页面
            Intent naviIntent = new Intent("android.intent.action.VIEW",
                    android.net.Uri.parse("baidumap://map/direction?origin=" + doubles[0] + "," +
                            doubles[1] + "&destination=" + address + "&mode=driving"));
            context.startActivity(naviIntent);
        } catch (Exception e) {
            ToastUtils.showShort("请先安百度地图客户端");
        }
    }

    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
     *
     * @param lat
     * @param lon
     */
    public static double[] gcj02_To_Bd09(double lat, double lon) {
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double tempLat = z * Math.sin(theta) + 0.006;
        double tempLon = z * Math.cos(theta) + 0.0065;
        double[] gps = {tempLat, tempLon};
        return gps;
    }
}
